﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using OgilvyOne.PreorderWap;

public partial class preview : System.Web.UI.Page
{
    protected string name = "";
    protected string sex = "";
    protected string mobile = "";
    protected string email = "";
    protected string budgetFrom = "";
    protected string budgetTO = "";
    protected string year = "";
    protected string month = "";
    protected string capcity = "";
    protected string model = "";
    protected string color = "";
    protected string seat = "";
    

    protected string source = "";
    protected string channel = "";
    protected void Page_Load(object sender, EventArgs e)
    {
            if (!string.IsNullOrEmpty(Request.QueryString["source"]))
                source = Request.QueryString["source"];
            if (!string.IsNullOrEmpty(Request.QueryString["channel"]))
                channel = Request.QueryString["channel"];


            if (!string.IsNullOrEmpty(Request.QueryString["name"]))
            {
                name = Request.QueryString["name"];
                sex = Request.QueryString["sex"];
                mobile = Request.QueryString["mobile"];
                email = Request.QueryString["email"];
                budgetFrom = Request.QueryString["budgetFrom"] + "万元";
                budgetTO = Request.QueryString["budgetTo"] + "万元";
                year = Request.QueryString["year"];
                month = Request.QueryString["month"];
                capcity = Request.QueryString["capcity"];
                model = Request.QueryString["model"];
                color = Request.QueryString["color"];
                seat = Request.QueryString["seat"];
            }
            else
            {
                Response.Redirect("Default.aspx");
            }
    }
    protected void button_edit_Click(object sender, EventArgs e)
    {
        string param = string.Format("name={0}&sex={1}&mobile={2}&email={3}&budgetFrom={4}&budgetTo={5}&year={6}&month={7}&capcity={8}&model={9}&color={10}&seat={11}&source={12}&channel={13}", name, sex, mobile, email, Request.QueryString["budgetFrom"], Request.QueryString["budgetTo"], year, month, capcity, model, Request.QueryString["color"], Request.QueryString["seat"], Request.QueryString["source"], Request.QueryString["channel"]);
        Response.Redirect("RegInfo.aspx?" + param + "&utm_source=" + Request.QueryString["source"] + "&utm_medium=" + Request.QueryString["channel"]);
    }
    protected void button_submit_Click(object sender, EventArgs e)
    {
        try
        {
            //正式
            string urlAddress = "http://vico1.vw.com.cn/newcms/testdrive/api/regsave.aspx";
            //测试
            //string urlAddress = "http://vico.ogilvy.com.cn/newcms/testdrive/api/regsave.aspx";
            int flag = ADD_Wap(name, sex, mobile, email, budgetFrom + "-" + budgetTO, year + month + "月", capcity, model, color, seat, Request.QueryString["source"], Request.QueryString["channel"]);

            //sendmail
            MailService mail = new MailService();
            string htmlMail = mail.generateEmail(color + "_mail.png", "email", name, sex, mobile, email, budgetFrom + budgetTO, year + month + "月", capcity, model, converColor(color), converSeat(seat));
            mail.sendMail(email, htmlMail);
            //api
            PostAPI post = new PostAPI();
            string strSeat = string.Empty;
            if (color == "jiaoshilan")
            {
                if (capcity == "1.4TSI")
                {
                    if (model == "时尚型")
                    {
                        strSeat = "";
                    }
                }
            }
            else
            {
                strSeat = converSeat(seat);
            }
            string result = post.Save(name, sex, mobile, email, year + month + "月", budgetFrom + "-" + budgetTO, Request.QueryString["source"], capcity, model, converColor(color), strSeat, Request.QueryString["channel"], urlAddress);
        }
        catch (Exception ex)
        {
            Response.Redirect("success.aspx?color=" + Request.QueryString["color"] + "&utm_source=" + Request.QueryString["source"] + "&utm_medium=" + Request.QueryString["channel"]);
        }
        Response.Redirect("success.aspx?color=" + Request.QueryString["color"] + "&utm_source=" + Request.QueryString["source"] + "&utm_medium=" + Request.QueryString["channel"]);
        
    }

    private int ADD_Wap(string name, string sex, string mobile, string email, string budget, string mention, string capcity, string carModel, string carColor, string carSeat,string source,string channel)
    {
        PreorderWapDAL use = new PreorderWapDAL();
        int resule = use.InsertWap(name, sex, mobile, email, budget, mention, capcity, carModel, converColor(carColor), converSeat(carSeat),source,channel);
        return resule;
    }

    public string converColor(string color)
    {
        string flag = "";
        switch (color)
        {
            case "jiaoshilan":
                flag = "金属漆_礁石蓝";
                break;
            case "lingyangbai":
                flag = "珠光漆_羚羊白";
                break;
            case "jufenghong":
                flag = "标准漆_飓风红";
                break;
            case "niuzailan":
                flag = "标准漆_牛仔蓝";
                break;
            case "tuxinghuang":
                flag = "标准漆_土星黄";
                break;
            case "xuanmuyin":
                flag = "金属漆_眩目银";
                break;
            case "tangguobai":
                flag = "标准漆_糖果白";
                break;
            case "zhenghei":
                flag = "标准漆_正黑";
                break;
            case "taifeihe":
                flag = "金属漆_太妃褐";
                break;
            case "shenhei":
                flag = "珠光漆_深黑";
                break;
            case "bojinhui":
                flag = "金属漆_铂金灰";
                break;
        }
        return flag;
    }

    public string converSeat(string seat)
    {
        string flag = "";
        switch (seat)
        {
            case "chunhei":
                flag = "真皮座椅_纯黑";
                break;
            case "mise":
                flag = "真皮座椅_米色";
                break;
            case "heilan":
                flag = "真皮座椅_黑色-蓝色(双色)";
                break;
            case "honghei":
                flag = "真皮座椅_红色-黑色(双色)";
                break;
            case "heise":
                flag = "织物座椅_黑色";
                break;
            case "shenmise":
                flag = "织物座椅_深米色";
                break;
        }
        return flag;
    }
}